package com.sgb.kjwl.view.ui.fragment.packers;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.packers.TeamMenuAdapter;
import com.sgb.kjwl.view.widget.transport.menu.LogisticsMenuAdapter;
import com.sgb.link.R;
import com.sgb.link.databinding.FragmentBigHomeMenuBinding;
import com.sgb.link.entity.MenuEntity;
import com.sgb.link.view.adapter.PageRecycleAdapter;
import com.sgb.link.view.fragment.AppLazyFragment;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("ValidFragment")
public class TeamMenuFragment extends AppLazyFragment {
    public static final String TAG = "MenuFra";
    public static final String KEY_DATA = "data";
    public static final String COLUMN_COUNT = "colum_count";
    public static final String ROW_COUNT = "row_count";
    public static final String PAGE = "page";

    FragmentBigHomeMenuBinding binding;
    private TeamMenuAdapter adapter;
    private List<AddressEntity> datas;
    private int colum = 4;
    private int row = 2;
    private int page;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public static TeamMenuFragment newInstance(List<AddressEntity> type, int colum, int row, int page) {
        TeamMenuFragment fragment = new TeamMenuFragment();
        fragment.datas=type;
        Bundle bundle = new Bundle();
        bundle.putInt(COLUMN_COUNT, colum);
        bundle.putInt(ROW_COUNT, row);
        bundle.putInt(PAGE, page);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        isNeedSetComponent = 1;
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_big_home_menu, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        colum = (int) getArguments().get(COLUMN_COUNT);
        row = (int) getArguments().get(ROW_COUNT);
        page = (int) getArguments().get(PAGE);
        init();
    }

    @Override
    protected void onLazyLoad() {

    }

    private void init() {
        adapter = new TeamMenuAdapter(getActivity(), null);
        binding.rlvHomeMenu.setAdapter(adapter);
        binding.rlvHomeMenu.setLayoutManager(new GridLayoutManager(getActivity(), colum));
        binding.rlvHomeMenu.addItemDecoration(new RecyclerCustomDecoration(
                0,
                0,
                0,
                getResources().getDimension(R.dimen.dp10)));
        adapter.setCount(colum * row);
        adapter.setDatas(datas);
        adapter.setOnItemClick(new PageRecycleAdapter.OnItemClick<AddressEntity>() {
            @Override
            public void onClick(AddressEntity entity, int position) {
                if (onItemClickCallBack != null) {
                    onItemClickCallBack.onClick(entity, position);
                    onItemClickCallBack.onClick(entity, position, colum * row * page + position);
                }
            }
        });

    }

    public OnItemClickCallBack onItemClickCallBack;

    @Override
    public void onClick(View view) {

    }

    public static abstract class OnItemClickCallBack {
        public void onClick(AddressEntity entity, int position) {
        }

        public void onClick(AddressEntity entity, int position, int num) {
        }
    }

    public void setOnItemClickCallBack(OnItemClickCallBack onItemClickCallBack) {
        this.onItemClickCallBack = onItemClickCallBack;
    }
}
